package com.example.tms.mapper;

import com.example.tms.bean.AreaInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface AreaMapper extends JpaRepository<AreaInfo,String> {

    //查询区域信息（客户id）
    List<AreaInfo> findByKhidAndSfczOrderByQynumDesc(String khid, boolean sfcz);

    //查询区域信息数（客户id）
    @Query(value = "select count(*) from area_info where khid = ?1 and sfcz = true", nativeQuery = true)
    int findCountByKhidAndSfcz(String khid);

    //查询所有区域信息
    @Query(value = "select * from area_info where sfcz = true", nativeQuery = true)
    List<AreaInfo>  findAll();

    //删除区域部分区域上移
    @Query(value = "update area_info set qynum = qynum-1 where khid = ?1 and  qynum>?2", nativeQuery = true)
    @Modifying
    @Transactional
    void updatePart(String khid, int qynum);

    //删除区域信息（假删除 区域id）
    @Query(value = "update area_info set qynum = 0, sfcz = false where qyid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void DeleteByQyid(String qyid);

    //查询区域信息（区域id）
    AreaInfo findByQyidAndSfcz(String qyid, boolean sfcz);

    //查询区域信息（客户id，区域）
    AreaInfo findByKhidAndQynum(String khid, int qynum);

    //区域上移
    @Query(value = "update area_info set qynum = qynum-1 where khid = ?1 and qyid = ?2", nativeQuery = true)
    @Modifying
    @Transactional
    void upArea(String khid, String qyid);

    //区域下移
    @Query(value = "update area_info set qynum = qynum+1 where khid = ?1 and qyid = ?2", nativeQuery = true)
    @Modifying
    @Transactional
    void downArea(String khid, String qyid);

    //查询区域信息（区域名称）
    AreaInfo findByQymc(String qymc);

}
